﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using XoLib.Serialization;

namespace XoLib.Menu
{

    /// <summary>
    /// Attribute to Control DocumentElement
    /// <example>  </example>/// 
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class StripFileKey : System.Attribute
    {

        /// <summary>
        /// Construtor
        /// <example> new StripFileKey("Vache"); </example>/// 
        /// </summary>
        public StripFileKey(string key)
        {
            this.key = key;
        }


        /// <summary>
        /// Property Key
        /// <example> sfk.Key </example>/// 
        /// </summary>
        public string key { get; private set; }

    }

#if WindowsCE || OldMenu
    /// <summary>
    /// ToolStripItems
    /// <example> new ToolStripItems() </example>/// 
    /// </summary>
    public class ToolStripItems : List<MenuItem>
    {

    }
#else
    /// <summary>
    /// ToolStripItems
    /// <example> new ToolStripItems() </example>/// 
    /// </summary>
    public class ToolStripItems : List<ToolStripItem>
    {
        
    }
#endif

    /// <summary>
    /// Build ToolStrip from file
    /// <example> new BuildStrip("File.xml") </example>/// 
    /// </summary>
   public abstract class BuildStrip
    {
          private ToolStripItems Menu = null;

          /// <summary>
          /// Resource to get menu icon
          /// </summary>
          public  System.ComponentModel.ComponentResourceManager resources = null;

          /// <summary>
          /// Constructor
          /// <example> new BuildStrip("File.xml") </example>/// 
          /// </summary>
          public BuildStrip(string filename)
          {
               Menu = Load(filename);
          }


        /// <summary>
        /// Get The DocumentElement
        /// <returns>string</returns>
        /// </summary>
        private string GetDocumentElementAttribute()
        {
            var Attributes = Attribute.GetCustomAttributes(this.GetType());
            foreach (var attri in Attributes)
            {
                if (attri is StripFileKey)
                    return ((StripFileKey)attri).key;
            }

            return null;
        }

#if WindowsCE || OldMenu
        /// <summary>
        /// Affect to the MenuStrip
        /// <para>
        /// <param name="menuStrip">MenuStrip</param>
        /// </para>
        /// <example> strip.Charger( Menu1 ); </example>/// 
        /// </summary>
        public void Charger(ref MainMenu menuStrip)
        {
            if (Menu == null)
                return;
#if OldMenuz
            menuStrip.MenuItems.AddRange(Menu.ToArray());
#else
            foreach (var m in Menu)
            {
                menuStrip.MenuItems.Add(m);
            }
#endif
        }
#else
        /// <summary>
        /// Affect to the MenuStrip
        /// <para>
        /// <param name="menuStrip">MenuStrip</param>
        /// </para>
        /// <example> strip.Charger( Menu1 ); </example>/// 
        /// </summary>
       public void Charger( ref MenuStrip menuStrip )
       {
           if(Menu != null)
                menuStrip.Items.AddRange(Menu.ToArray());
       }
#endif

       /// <summary>
       /// Affect to the MenuStrip
       /// <para>
       /// <param name="filename">string</param>
       /// </para>
       /// <example> Menu = Load("File.xml"); </example>/// 
       /// <returns>ToolStripItems</returns>
       /// </summary>
       public ToolStripItems Load(string filename)
        {
            ConfigXML cx = new ConfigXML();
            if (!cx.Load(filename, GetDocumentElementAttribute()))
                return null;

            return Parcourir(cx.GetChilds());        
        }

       /// <summary>
       /// Parse xml File
       /// <para>
       /// <param name="nodelist">XmlNodeList</param>
       /// </para>
       /// <example>  return Parcourir(cx.GetChilds());  </example>/// 
       /// <returns>ToolStripItems</returns>
       /// </summary>
       public abstract ToolStripItems Parcourir(XmlNodeList nodelist);


#if !WindowsCE 
       /// <summary>
       /// Parse key string
       /// <para>
       /// <param name="Keystring">string</param>
       /// </para>
       /// <example> key = ParseKeys("Control+B"); </example>/// 
       /// <returns>Keys</returns>
       /// </summary>
        public Keys ParseKeys(string Keystring)
        {
            Keys KeysValue = Keys.None;
            Keys RetKeys = Keys.None;
            if (Keystring != null)
            {
                string[] array = Keystring.Split('+');
                foreach (string scell in array)
                {
                    
#if NET45
                    if (Enum.TryParse(scell, out KeysValue))
                    {
                        RetKeys = RetKeys | KeysValue;
                    }
#else
                    try
                    {
                        KeysValue = (Keys)Enum.Parse(typeof(Keys), scell, true);
                        RetKeys = RetKeys | KeysValue;
                    }
                    catch
                    {
                    }
#endif
                }
            }

            return RetKeys;
        }
#endif
#if OldMenu 
       /// <summary>
       /// Parse key string
       /// <para>
       /// <param name="Keystring">string</param>
       /// </para>
       /// <example> key = ParseKeys("Control+B"); </example>/// 
       /// <returns>Keys</returns>
       /// </summary>
       public Shortcut ParseKeys(string Keystring)
       {
           Shortcut KeysValue = Shortcut.None;
           Shortcut RetKeys = Shortcut.None;
           if (Keystring != null)
           {
               string[] array = Keystring.Split('+');
               foreach (string scell in array)
               {

#if dNET45
                   if (Enum.TryParse(scell, out KeysValue))
                   {
                       RetKeys = RetKeys | KeysValue;
                   }
#else
                    try
                    {
                        KeysValue = (Shortcut)Enum.Parse(typeof(Keys), scell, true);
                        RetKeys = RetKeys | KeysValue;
                    }
                    catch
                    {
                    }
#endif
               }
           }

           return RetKeys;
       }
#endif


        /// <summary>
        ///  Capture Menu click
        /// <para>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        /// </para>
        /// </summary>
        protected abstract internal void BuildStrip_Click(object sender, EventArgs e);

    }
}
